package com.zxc.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxc.reggie.common.R;
import com.zxc.reggie.dto.SetmealDto;
import com.zxc.reggie.entity.Category;
import com.zxc.reggie.entity.Setmeal;
import com.zxc.reggie.entity.SetmealDish;
import com.zxc.reggie.service.CategoryService;
import com.zxc.reggie.service.SetMealDishService;
import com.zxc.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Resource
    private SetMealService setMealService;

    @Resource
    private SetMealDishService setMealDishService;

    @Resource
    private CategoryService categoryService;

    @PostMapping()
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        return setMealService.saveWithCategoryName(setmealDto);
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {

        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(name != null, Setmeal::getName, name).orderByDesc(Setmeal::getUpdateTime);
        setMealService.page(setmealPage, setmealLambdaQueryWrapper);
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<SetmealDto> setmealDtoPageRecords = setmealPage.getRecords().stream().map(item -> {
            Category category = categoryService.query().eq("id", item.getCategoryId()).one();
            String setmealDishName = category.getName();
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            setmealDto.setCategoryName(setmealDishName);
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoPageRecords);
        return R.success(setmealDtoPage);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> findSetMealDto(@PathVariable String id) {

        Setmeal setmeal = setMealService.query().eq("id", id).one();

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        Long categoryId = setmealDto.getCategoryId();


        Category category = categoryService.query().eq("id", categoryId).one();
        List<SetmealDish> setmealDishes = setMealDishService.query().eq("setmeal_id", id).list();
        setmealDto.setSetmealDishes(setmealDishes);
        setmealDto.setCategoryName(category.getName());
        return R.success(setmealDto);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {

        return setMealService.removeWithDish(ids);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {

        setMealService.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setMealDishService.remove(setmealDishLambdaQueryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().peek(item -> {
            item.setSetmealId(setmealDto.getId());
        }).collect(Collectors.toList());
        setMealDishService.saveBatch(setmealDishes);

        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId) {
        List<Setmeal> setmealList = setMealService.query().eq(categoryId != null, "category_id", categoryId).list();
        return R.success(setmealList);
    }

}
